package com.hdoit.ares.web.acc.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.acc.model.AccountDetail;
import com.hdoit.ares.logical.acc.service.IAccountDetailService;
import com.hdoit.ares.logical.acc.service.IAccountInfoService;
import com.hdoit.ares.logical.viewmodel.acc.AccountDetailSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      caochengmeng
 * @email       
 * @createtime  2017年11月27日 20:07:02
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/acc/accountDetail/")
public class AccountDetailController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas//admin/acc/accountDetail/";
    }

    private IAccountDetailService accountDetailService;
    public IAccountDetailService getAccountDetailService() {
        return accountDetailService;
    }
    @Resource
    public void setAccountDetailService(IAccountDetailService accountDetailService) {
        this.accountDetailService = accountDetailService;
    }
    
    private IAccountInfoService accountInfoService;
    public IAccountInfoService getAccountInfoService() {
		return accountInfoService;
	}
    @Resource
	public void setAccountInfoService(IAccountInfoService accountInfoService) {
		this.accountInfoService = accountInfoService;
	}

	private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="accountDetailDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,AccountDetailSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(accountDetailService.queryCount(search));
        List<AccountDetail> accountDetails= accountDetailService.queryList(search,pagerModel);
        
        Double allBalance = accountInfoService.queryAllBalance();
       
        model.addAttribute("allBalance",allBalance);
        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("accountDetails", accountDetails);
        model.addAttribute("accountDetailSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String accountDetailDetailed(Model model,String key){
        AccountDetail accountDetail=new AccountDetail();
        if(!StringUtils.isEmpty(key)){
            accountDetail=accountDetailService.queryModel(key);
        }
        model.addAttribute("accountDetail", accountDetail);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,AccountDetail accountDetail){
        if(StringUtils.isEmpty(accountDetail.getId())){
            accountDetail.setId(CommonUtils.getKeyId());
            accountDetail.setModelStatus(ModelStatus.add);
        }else{
            accountDetail.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(accountDetailService.applyChange(accountDetail)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("accountDetail", accountDetail);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(accountDetailService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}
